package com.zhengsr.latte.ui.recycler.adapter.base;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhengsr.latte.ui.recycler.adapter.callback.RBaseListenerAdapter;


public class RBaseViewholder extends RecyclerView.ViewHolder {
    private static final String TAG = "RBaseViewholder";
    private SparseArray<View> mViewSparseArray; //用来保存 itemview；
    private View mConserView;
    private RBaseAdapter mRBaseAdapter;
    private Context mContext;
    public RBaseViewholder(View itemView) {
        super(itemView);
        mViewSparseArray = new SparseArray<>();
        mConserView = itemView;
        mContext = itemView.getContext();
    }

    public void setAdapter(RBaseAdapter adapter){
        mRBaseAdapter = adapter;
    }


    /**
     * 这里相当于在 oncreateViewholder 中，设置好数据，然后返回
     * @param context
     * @param layoutid
     * @param parent
     * @return
     */
    public static RBaseViewholder getViewHolder(Context context, int layoutid, ViewGroup parent){
        View view = LayoutInflater.from(context).inflate(layoutid,parent,false);
        RBaseViewholder viewholder = new RBaseViewholder(view);
        return viewholder;
    }

    /**
     * 这里相当于在 oncreateViewholder 中，设置好数据，然后返回
     * @param context
     * @param view
     * @return
     */
    public static RBaseViewholder getViewHolder(Context context, View view){
        RBaseViewholder viewholder = new RBaseViewholder(view);
        return viewholder;
    }


    public View getConserView(){
      return mConserView;
    }



    /**
     * getItemview，通过 id获取view
     * @param viewId
     * @return
     */
    public <T extends View>T getItemView(int viewId){
        View view = mViewSparseArray.get(viewId);
        if (view == null){
            view = mConserView.findViewById(viewId);
            mViewSparseArray.put(viewId,view);
        }
        return (T) view;
    }

    /**
     * 设置textview
     */
    public RBaseViewholder setText(int viewId,String data){
        TextView textView = (TextView) getItemView(viewId);
        textView.setText(data);
        return this;
    }
    /**
     * 设置textview
     */
    public RBaseViewholder setText(int viewId,int resId){
        TextView textView = (TextView) getItemView(viewId);
        textView.setText(mContext.getString(resId));
        return this;
    }

    /**
     * 设置 imageview
     */
    public RBaseViewholder setDrawable(int viewid,Drawable drawable){
        ImageView imageView = getItemView(viewid);
        imageView.setImageDrawable(drawable);
        return this;
    }

    /**
     * 设置 imageview
     */
    public RBaseViewholder setBitmap(int viewid,Bitmap bitmap){
        ImageView imageView = getItemView(viewid);
        imageView.setImageBitmap(bitmap);
        return this;
    }


    /**
     * 监听子view的点击事件
     * @param viewId
     * @return
     */
    public RBaseViewholder addChildClickListener(int viewId){
        final View view = getItemView(viewId);
        if (view != null) {
            if (!view.isClickable()){
                view.setClickable(true);
            }
            final RBaseListenerAdapter listenerAdapter = mRBaseAdapter.getListener();
            if (listenerAdapter != null){
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listenerAdapter.onItemChildClick(view,
                                RBaseViewholder.this.getLayoutPosition());
                    }
                });

            }
            
        }
        return this;
    }

    /**
     * 监听子view的长按事件
     * @param viewId
     * @return
     */
    public RBaseViewholder addChildLongClickListener(int viewId){
        final View view = getItemView(viewId);
        if (view != null) {
            if (!view.isClickable()){
                view.setClickable(true);
            }
            final RBaseListenerAdapter listenerAdapter = mRBaseAdapter.getListener();
            if (listenerAdapter != null){
                view.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        return listenerAdapter.onItemChildLongClick(view,
                                RBaseViewholder.this.getLayoutPosition());
                    }
                });

            }
        }
        return this;
    }


}